﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using PlugNT.Cms.Model;
using PlugNT.Cms.BLL;
using PlugNT.Cms.Page;

namespace PlugNT.Cms.Web.Admin.Pre_Advertise
{
    public partial class Edit : AdminPage
    {
        protected string adsType = string.Empty;
        private Advertise curAdvertise = new Advertise();
        private FriendLinkClass curAdvertiseClass = new FriendLinkClass();
        private string id = "";
        private string classno = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            //权限检测
            this.CheckPower();

            id = (Request.QueryString["id"] == null) ? "" : Request.QueryString["id"].Trim();
            classno = (Request.QueryString["clsno"] == null) ? "" : Request.QueryString["clsno"].Trim();
            adsType = ChangeHope.WebPage.PageRequest.GetQueryString("type") == "" ? "0" : ChangeHope.WebPage.PageRequest.GetQueryString("type");
            ViewState["adtype"] = adsType;
            if (!IsPostBack)
            {
                //上次请求路径
                if (hidReferrer.Value.Trim() == "")
                {
                    Uri uri = Request.UrlReferrer;
                    if (uri != null)
                    {
                        hidReferrer.Value = uri.ToString();
                    }
                }

                InitData();
            }
        }



        private void InitData()
        {

            if (id != "")
            {
                int tid = int.Parse(id);
                AdvertiseInfo model = curAdvertise.GetAdvertiseInfo(tid);
                if (model != null)
                {

                    #region 显示要编辑的数据
                    if (ChangeHope.WebPage.PageRequest.GetQueryInt("id") > 0)
                    {
                        this.txtName.Text = model.name;
                        this.txtPower.Text = model.power.ToString();
                        this.txtBrowseCount.Text = model.browsecount.ToString();
                        this.txtClickCount.Text = model.clickcount.ToString();
                        this.dpStart.Text = Convert.ToDateTime(model.overduetime).ToShortDateString();
                        this.chxExamine.Checked = model.examine == 1 ? true : false;
                        this.chxClickCount.Checked = model.statclick == 1 ? true : false;
                        this.chxBorwsCount.Checked = model.statbrowse == 1 ? true : false;
                        if (model.adtype == 0)
                        {
                            this.pnlType1.Visible = true;
                            this.txtWidth1.Text = model.sizebreadth;
                            this.txtHeight1.Text = model.hight;
                            this.txtLinkAddress.Text = model.linkaddress;
                            this.txtHint1.Text = model.hint;
                            this.rdolstTarget1.SelectedValue = model.backgortarget.ToString();
                            this.txtContent1.Text = model.advertisecont;
                            this.txtUpspread1.Text = model.upspreadadd;
                            ViewState["adtype2"] = model.adtype.ToString();
                            ViewState["picAddress"] = model.upspreadadd;
                        }
                        else if (model.adtype == 1)
                        {
                            this.pnlType1.Visible = false;
                            this.pnlType2.Visible = true;
                            this.txtWidth2.Text = model.sizebreadth;
                            this.txtHeight2.Text = model.hight;
                            this.rlistTarget2.SelectedValue = model.backgortarget.ToString();
                            this.chxClickCount.Enabled = false;
                            this.txtClickCount.Enabled = false;
                            ViewState["adtype2"] = model.adtype.ToString();
                            ViewState["picAddress"] = model.upspreadadd;

                        }
                        else if (model.adtype == 2)
                        {
                            this.pnlType1.Visible = false;
                            this.pnlType2.Visible = false;
                            this.pnlType3.Visible = true;
                           
                            this.txtContent3.Text = model.advertisecont;
                            this.chxClickCount.Enabled = false;
                            this.txtClickCount.Enabled = false;
                            ViewState["adtype2"] = model.adtype.ToString();

                        }
                        else if (model.adtype == 3)
                        {
                            this.pnlType4.Visible = true;
                            this.pnlType1.Visible = false;
                            this.pnlType2.Visible = false;
                            this.pnlType3.Visible = false;
                            this.txtWidth4.Text = model.sizebreadth;
                            this.txtHeight4.Text = model.hight;
                            this.txtLinkAddress4.Text = model.linkaddress;
                            this.txtHint4.Text = model.hint;
                            this.rlistTarget4.SelectedValue = model.backgortarget.ToString();
                            this.txtContent4.Text = model.advertisecont;
                            this.txtUpspread4.Text = model.upspreadadd;
                            ViewState["adtype2"] = model.adtype.ToString();
                            ViewState["picAddress"] = model.upspreadadd;
                        }
                    }
                    #endregion

                    #region 添加数据
                    else
                    {
                        this.dpStart.Text = DateTime.Now.ToShortDateString();
                        this.txtClickCount.Text = "0";
                        this.txtBrowseCount.Text = "0";
                    }
                    #endregion

                }
                else
                {
                    Response.Redirect("../Message.aspx?message=修改链接失败，未找到相对应的链接！&z=no");
                }
            }
            else
            {
                Response.Redirect("../Message.aspx?message=修改链接失败，未从正确的地址编辑！&z=no");
            }
        }

        protected void btnModify_Click(object sender, EventArgs e)
        {
            Save();
        }

        #region 判断广告类型 只有图片类型才统计点击数

        protected void CheckType(string type)
        {
            if (type == "0")
            {
                this.pnlType1.Visible = true;
                this.pnlType2.Visible = false;
                this.pnlType3.Visible = false;
                this.pnlType4.Visible = false;
                this.chxClickCount.Enabled = true;
                this.txtClickCount.Enabled = true;
            }
            else if (type == "1")
            {
                this.pnlType1.Visible = false;
                this.pnlType3.Visible = false;
                this.pnlType2.Visible = true;
                this.pnlType4.Visible = false;
                this.chxClickCount.Enabled = false;
                this.txtClickCount.Enabled = false;
            }
            else if (type == "2")
            {
                this.pnlType1.Visible = false;
                this.pnlType2.Visible = false;
                this.pnlType3.Visible = true;
                this.pnlType4.Visible = false;
                this.chxClickCount.Enabled = false;
                this.txtClickCount.Enabled = false;
            }
            else if (type == "3")
            {
                this.pnlType1.Visible = false;
                this.pnlType2.Visible = false;
                this.pnlType3.Visible = false;
                this.pnlType4.Visible = true;
                this.chxClickCount.Enabled = false;
                this.txtClickCount.Enabled = false;
            }
            else if (type == "4")
            {
                this.pnlType1.Visible = false;
                this.pnlType2.Visible = false;
                this.pnlType3.Visible = false;
                this.pnlType4.Visible = true;
                this.chxClickCount.Enabled = false;
                this.txtClickCount.Enabled = false;
            }
        }
        #endregion

        #region 图片广告 图片上传
        protected void btnFileUpload_Click(object sender, EventArgs e)
        {
            if (FileUpload1.HasFile)
            {
                ChangeHope.Common.UploadFile uf = new ChangeHope.Common.UploadFile();
                uf.ExtensionLim = ".gif,.jpg,.jpeg,.bmp";
                uf.FileLengthLim = 4000;
                uf.PostedFile = this.FileUpload1;

                uf.SavePath = "/UploadFiles/advertise";
                if (uf.Upload())
                {
                    if (uf.HaveLoad)
                    {
                        if (txtUpspread1.Text == "")
                        {
                            this.txtUpspread1.Text = uf.FilePath;
                        }
                        else
                        {
                            this.txtUpspread1.Text = txtUpspread1.Text.ToString() + "|" + uf.FilePath;
                        }
                    }
                    else
                    {
                        return;
                    }
                }
                else
                {

                    return;
                }
            }
        }
        #endregion
        #region 删除一张图片

        /// <summary>
        /// 删除一张图片
        /// </summary>
        /// <param name="arrPicList"></param>
        /// <returns></returns>
        protected string delPic(string PicList)
        {
            String[] arrPic = PicList.Split('|');
            string strList = "";
            if (arrPic.Length == 1 || arrPic.Length < 1)
            {
                strList = "";
            }
            else
            {
                for (int i = 0; i < arrPic.Length - 1; i++)
                {

                    string arrTenp1 = arrPic[i].ToString();
                    string arrTenp2;
                    if (i > 0)
                    {
                        arrTenp2 = arrPic[i].ToString();
                    }
                    else
                    {
                        arrTenp2 = arrPic[i].ToString();
                    }
                    if (i == 0)
                    {
                        strList = arrTenp1;
                    }
                    else
                    {
                        if (strList == "")
                        {
                            strList = arrTenp1;
                        }
                        else
                        {
                            strList = strList + "|" + arrTenp1;
                        }
                    }

                }
            }
            return strList;
        }

        /// <summary>
        ///删除图片广告 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnDelPic_Click(object sender, EventArgs e)
        {
            txtUpspread1.Text = this.delPic(txtUpspread1.Text.ToString());
        }
        #endregion

        #region 幻灯片广告图片上传
        protected void btnUploadSlide_Click(object sender, EventArgs e)
        {
            if (FileUpload3.HasFile)
            {
                //原图
                ChangeHope.Common.UploadFile uf = new ChangeHope.Common.UploadFile();
                uf.ExtensionLim = ".gif,.jpg,.jpeg,.bmp";
                uf.FileLengthLim = 4000;
                uf.PostedFile = this.FileUpload3;
                //暂时存放在这里
                uf.SavePath = "/UploadFiles/Advertise";
                if (uf.Upload())
                {
                    if (uf.HaveLoad)
                    {
                        if (txtUpspread4.Text == "")
                        {
                            this.txtUpspread4.Text = uf.FilePath;
                        }
                        else
                        {
                            this.txtUpspread4.Text = txtUpspread4.Text.ToString() + "|" + uf.FilePath;
                        }
                    }
                    else
                    {

                        return;
                    }
                }
                else
                {

                    return;
                }
            }
        }
        #endregion

        #region 删除一张幻灯片广告图片
        protected void btnDelSlide_Click(object sender, EventArgs e)
        {
            this.txtUpspread4.Text = this.delPic(this.txtUpspread4.Text.ToString());
        }
        #endregion

        #region 保存操作
        private void Save()
        {
            Advertise bll = new Advertise();
            int tid = int.Parse(id);
            AdvertiseInfo model = curAdvertise.GetAdvertiseInfo(tid);
            model.id = tid;
            model.name = this.txtName.Text.Trim();
            model.power = this.txtPower.Text.Length == 0 ? 0 : Convert.ToInt32(this.txtPower.Text.Trim());
            model.browsecount = this.txtBrowseCount.Text.Length == 0 ? 0 : Convert.ToInt32(this.txtBrowseCount.Text.Trim());
            model.clickcount = this.txtClickCount.Text.Length == 0 ? 0 : Convert.ToInt32(this.txtClickCount.Text.Trim());
            try
            {
                model.overduetime = Convert.ToDateTime(this.dpStart.Text.Trim());
            }
            catch
            {
                Response.Redirect("../Message.aspx?message=请输入正确的时间格式，最好通过选择器选择日期！&z=no");
                return;
            }

            model.examine = this.chxExamine.Checked ? 1 : 0;
            model.statclick = this.chxClickCount.Checked ? 1 : 0;
            model.statbrowse = this.chxBorwsCount.Checked ? 1 : 0;

            #region 修改操作
            if (Request["id"] != null)
            {

                if (ViewState["adtype"] != null)
                {
                    //图片
                    if (ViewState["adtype"].ToString() == "0")
                    {
                        model.sizebreadth = txtWidth1.Text.Trim();
                        model.hight = txtHeight1.Text.Trim();
                        model.linkaddress = txtLinkAddress.Text.Trim();
                        model.hint = txtHint1.Text.Trim();
                        //打开方式
                        model.backgortarget = Convert.ToInt32(this.rdolstTarget1.SelectedValue);
                        model.advertisecont = txtContent1.Text.Trim();
                        model.adtype = 0;
                        model.id = tid;
                        if (txtUpspread1.Text != "")
                        {
                            model.upspreadadd = this.txtUpspread1.Text.ToString();
                        }
                        else
                        {
                            model.upspreadadd = ViewState["picAddress"].ToString();
                        }
                        bll.Update(model);
                        Response.Redirect("../Message.aspx?message=修改链接成功！" + "&z=yes");
                    }
                    //动画
                    else if (ViewState["adtype"].ToString() == "1")
                    {
                        ViewState["adtype2"] = "1";
                        string Upspread = string.Empty;

                        if (FileUpload2.HasFile)
                        {
                            ChangeHope.Common.UploadFile uf = new ChangeHope.Common.UploadFile();
                            uf.ExtensionLim = ".gif,.swf";
                            uf.FileLengthLim = 4000;
                            uf.PostedFile = this.FileUpload2;
                            uf.SavePath = "/UploadFiles/Advertise";
                            if (uf.Upload())
                            {
                                if (uf.HaveLoad)
                                {
                                    Upspread = uf.FilePath;
                                }
                                else
                                {
                                    Response.Redirect("../Message.aspx?message=修改链接成功！" + "&z=yes");
                                    return;
                                }
                            }
                            else
                            {
                                Response.Redirect("../Message.aspx?message=修改链接成功！" + "&z=yes");
                                return;
                            }
                        }
                        model.sizebreadth = this.txtWidth2.Text.Trim();
                        model.hight = this.txtHeight2.Text.Trim();
                        model.linkaddress = "";
                        model.hint = "";
                        model.backgortarget = Convert.ToInt32(this.rlistTarget2.SelectedValue);
                        model.advertisecont = "";
                        model.upspreadadd = Upspread;
                        model.adtype = 1;
                        model.id = tid;
                        bll.Update(model);
                        Response.Redirect("../Message.aspx?message=修改链接成功！" + "&z=yes");
                    }
                    //文本
                    else if (ViewState["adtype"].ToString() == "2")
                    {
                        model.linkaddress = "";
                        model.hint = "";
                        model.sizebreadth = "";
                        model.hight = "";
                        model.backgortarget = 0;
                        model.upspreadadd = "";
                        model.adtype = 2;
                        model.advertisecont = this.txtContent3.Text.Trim();
                        model.id = tid;
                        bll.Update(model);
                        Response.Redirect("../Message.aspx?message=修改链接成功！" + "&z=yes");
                    }
                    //幻灯片
                    else if (ViewState["adtype"].ToString() == "3")
                    {
                        model.sizebreadth = this.txtWidth4.Text.Trim();
                        model.hight = this.txtHeight4.Text.Trim();
                        model.linkaddress = this.txtLinkAddress4.Text.Trim();
                        model.hint = this.txtHint4.Text.Trim();
                        model.backgortarget = Convert.ToInt32(this.rlistTarget4.SelectedValue);
                        model.advertisecont = this.txtContent4.Text.Trim();
                        if (this.txtUpspread1.Text != "")
                        {
                            model.upspreadadd = this.txtUpspread4.Text.ToString();
                        }
                        else
                        {
                            model.upspreadadd = ViewState["picAddress"].ToString();
                        }
                        model.adtype = 3;
                        model.id = tid;
                        bll.Update(model);
                        Response.Redirect("../Message.aspx?message=修改链接成功！" + "&z=yes");

                    }
                }

            }
            #endregion
            #region 增加操作
            else
            {
                #region 图片类型广告
                if (ViewState["adtype"] == null || ViewState["adtype"].ToString() == "0")
                {
                    model.sizebreadth = this.txtWidth1.Text.Trim();
                    model.hight = this.txtHeight1.Text.Trim();
                    model.linkaddress = this.txtLinkAddress.Text.Trim();
                    model.hint = this.txtHint1.Text.Trim();
                    //打开方式
                    model.backgortarget = Convert.ToInt32(this.rdolstTarget1.SelectedValue);
                    model.advertisecont = this.txtContent1.Text.Trim();
                    model.upspreadadd = this.txtUpspread1.Text.ToString();
                    model.adtype = 0;
                    bll.Add(model);
                    //操作成功，已保存该信息"; 
                }
                #endregion

                #region 动画类型广告
                else if (ViewState["adtype"].ToString() == "1")
                {
                    string Upspread = string.Empty;
                    if (FileUpload2.HasFile)
                    {
                        ChangeHope.Common.UploadFile uf = new ChangeHope.Common.UploadFile();
                        uf.ExtensionLim = ".gif,.swf";
                        uf.FileLengthLim = 4000;
                        uf.PostedFile = this.FileUpload2;
                        uf.SavePath = "/UploadFiles/advertise";
                        if (uf.Upload())
                        {
                            if (uf.HaveLoad)
                            {
                                Upspread = uf.FilePath;
                            }
                            else
                            {

                                return;
                            }
                        }
                        else
                        {

                            return;
                        }
                    }
                    model.sizebreadth = this.txtWidth2.Text.Trim();
                    model.hight = this.txtHeight2.Text.Trim();
                    model.linkaddress = "";
                    model.hint = "";
                    model.backgortarget = Convert.ToInt32(this.rlistTarget2.SelectedValue);
                    model.advertisecont = "";
                    model.upspreadadd = Upspread;
                    model.adtype = 1;
                    bll.Add(model);
                    //"操作成功，已保存该信息";
                }
                #endregion

                #region 文本类型广告
                else if (ViewState["adtype"].ToString() == "2")
                {
                    model.linkaddress = "";
                    model.hint = "";
                    model.sizebreadth = "";
                    model.hight = "";
                    model.backgortarget = 0;
                    model.upspreadadd = "";
                    model.adtype = 2;
                    model.advertisecont = this.txtContent3.Text.Trim();
                    bll.Add(model);
                    //"操作成功，已保存该信息";

                }
                #endregion

                #region 幻灯片类型广告
                else if (ViewState["adtype"].ToString() == "3")
                {
                    model.sizebreadth = this.txtWidth4.Text.Trim();
                    model.hight = this.txtHeight4.Text.Trim();
                    model.linkaddress = this.txtLinkAddress4.Text.Trim();
                    model.hint = this.txtHint4.Text.Trim();
                    model.backgortarget = Convert.ToInt32(this.rlistTarget4.SelectedValue);
                    model.advertisecont = this.txtContent4.Text.Trim();
                    model.upspreadadd = this.txtUpspread4.Text.ToString();
                    model.adtype = 3;
                    bll.Add(model);
                }
                #endregion
            }
            #endregion
        }
        #endregion
    }
}
